<template>
  <div class="card task-list">
    <div class="card-header" @click="showTaskDetail">
      <h3><i class="el-icon-tickets"></i> 我的待办</h3>
      <el-button type="text" @click.stop="handleAdd">+ 新建</el-button>
    </div>
    <div class="card-body">
      <div class="task-items">
        <div v-for="(task, index) in visibleTasks" :key="index" class="task-item" :class="`priority-${task.priority}`">
          <el-checkbox v-model="task.completed" />
          <div class="task-content">
            <div class="task-title">
              <span :class="{ completed: task.completed }">{{ task.title }}</span>
              <el-tag v-if="task.tag" size="small" :type="tagTypes[task.tag.type]">
                {{ task.tag.text }}
              </el-tag>
            </div>
            <div class="task-meta">
              <span class="deadline">
                <i class="el-icon-time"></i>
                {{ task.deadline }}
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="card-footer">
        <el-button type="text" @click="showTaskDetail">查看全部</el-button>
      </div>
    </div>

    <!-- 待办事项详情弹窗 -->
    <TaskDetail 
      :visible="isTaskDetailVisible" 
      :tasks="tasks"
      @close="hideTaskDetail"
      @add-task="handleAdd"
    />
  </div>
</template>

<script>
import TaskDetail from './TaskDetail.vue'

export default {
  name: 'TaskList',
  components: {
    TaskDetail
  },
  data() {
    return {
      isTaskDetailVisible: false,
      maxVisibleTasks: 3,
      tagTypes: {
        urgent: 'danger',
        important: 'warning',
        normal: 'info'
      },
      tasks: [
        {
          title: '完成季度财务报告',
          completed: false,
          priority: 1,
          deadline: '今天 18:00',
          tag: { type: 'urgent', text: '紧急' }
        },
        {
          title: '审批采购申请单',
          completed: false,
          priority: 2,
          deadline: '明天',
          tag: { type: 'important', text: '重要' }
        },
        {
          title: '更新项目计划表',
          completed: true,
          priority: 3,
          deadline: '2023-06-15'
        },
        {
          title: '准备部门会议材料',
          completed: false,
          priority: 2,
          deadline: '明天 10:00'
        },
        {
          title: '提交周工作报告',
          completed: false,
          priority: 3,
          deadline: '每周五'
        }
      ]
    }
  },
  computed: {
    visibleTasks() {
      return this.tasks.slice(0, this.maxVisibleTasks)
    }
  },
  methods: {
    showTaskDetail() {
      this.isTaskDetailVisible = true
    },
    hideTaskDetail() {
      this.isTaskDetailVisible = false
    },
    handleAdd() {
      this.$message.info('打开新建任务对话框')
      // 实际项目中这里可以打开新建任务表单
    }
  }
}
</script>

<style scoped>
.task-list {
  background: #fff;
  border: 1px solid #e0e0e0;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #e0e0e0;
  background: #f8f9fa;
  cursor: pointer;
  transition: background 0.2s;
}

.card-header:hover {
  background-color: #f0f2f5;
}

.card-header h3 {
  margin: 0;
  font-size: 15px;
  color: #333;
  display: flex;
  align-items: center;
}

.card-header h3 i {
  margin-right: 8px;
  color: #666;
}

.card-body {
  padding: 0;
}

.task-items {
  padding: 8px 0;
}

.task-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  transition: background 0.3s;
  border-left: 3px solid transparent;
}

.task-item:hover {
  background: #f5f7fa;
}

.task-item.priority-1 {
  border-left-color: #f5222d;
}

.task-item.priority-2 {
  border-left-color: #fa8c16;
}

.task-item.priority-3 {
  border-left-color: #1890ff;
}

.task-content {
  flex: 1;
  margin-left: 12px;
}

.task-title {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}

.task-title span {
  margin-right: 8px;
}

.task-title .completed {
  text-decoration: line-through;
  color: #999;
}

.task-meta {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #666;
}

.deadline {
  display: flex;
  align-items: center;
}

.deadline i {
  margin-right: 4px;
  font-size: 12px;
}

.card-footer {
  padding: 8px 16px;
  border-top: 1px solid #e0e0e0;
  text-align: center;
  background: #f8f9fa;
}
</style>